package com.hethongviet.vending.pages;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.hethongviet.vending.AppConfig;
import com.google.android.DemoKit.R;
import com.hethongviet.vending.Utils;
import com.hethongviet.vending.helper.call.ListenPhoneCallService;
import com.hethongviet.vending.helper.call.OnIncomingCall;
import com.hethongviet.vending.helper.dialog.DialogMessageBack;
import com.hethongviet.vending.model.Transaction;
import com.hethongviet.vending.network.CONSTANTS.NETWORK_ERROR;
import com.hethongviet.vending.network.CONSTANTS.STATUS;
import com.hethongviet.vending.network.Network;
import com.hethongviet.vending.network.OnTimeOutListener;

public abstract class GeneralActivity extends Activity implements
		OnTimeOutListener, OnIncomingCall
{

	public static final char CODE_END = '#';
	public static final String CODE_OK = "OK";
	private static final String TAG = "GeneralActivity";

	protected ProgressDialog _progressDialog = null;

	public void showProgress(Activity activity)
	{
		_progressDialog = ProgressDialog.show(this, "",
				activity.getString(R.string.please_wait), true, false);
	}

	public void hideProgressDialog()
	{
		if (_progressDialog != null)
		{
			_progressDialog.dismiss();
			_progressDialog = null;
		}
	}

	public abstract void onConnectFail(int typeError);

	public abstract void onNetworkMessageIncomming(Transaction transaction);

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		/* Full screen */
		Utils.prepareWindow(this);

	}

	@Override
	protected void onResume()
	{
		hideProgressDialog();

		ListenPhoneCallService.setOnIncomingCall(this);

		Network.setOnNetworkHandleListener(this);

		super.onResume();

	}

	public Dialog createDialogStatusServer(int status)
	{
		switch (status)
		{
			case R.string.message_server_send_ok:
				return new DialogMessageBack(this, false,
						R.string.message_server_send_ok, false);
			default:
				return new DialogMessageBack(this, true, status, false);
		}
	}

	public void showDialogWithCatch(final int id)
	{
		runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				try
				{
					showDialog(id);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});

	}

	public void showDialogWithStatus(final int status)
	{
		runOnUiThread(new Runnable()
		{

			@Override
			public void run()
			{
				if (AppConfig.IS_DEBUG)
				{
					switch (status)
					{
						case STATUS.SERVER_DB_CREATE_FAIL:
							showDialogWithCatch(R.string.message_server_fail_create_db);
							break;
						case STATUS.SERVER_DB_INSERT_FAIL:
							showDialogWithCatch(R.string.message_server_fail_insert_db);
							break;
						case STATUS.SERVER_SEND_FAIL_DB:
							showDialogWithCatch(R.string.message_server_send_fail_db);
							break;
						case STATUS.SERVER_SEND_FAIL_DB_LOAD:
							showDialogWithCatch(R.string.message_server_send_fail_db_load);
							break;
						case STATUS.SERVER_SEND_FAIL_JSON:
							showDialogWithCatch(R.string.message_server_send_fail_json_load);
							break;
						case STATUS.SERVER_SEND_FAIL_JSON_VALUE:
							showDialogWithCatch(R.string.message_server_send_fail_json_value);
							break;
						case STATUS.SERVER_SEND_FAIL_VALUE_NULL:
							showDialogWithCatch(R.string.message_server_send_fail_value_null);
							break;
						case STATUS.SERVER_SEND_OK:
							showDialogWithCatch(R.string.message_server_send_ok);
							break;
						case NETWORK_ERROR.CONNECT:
							showDialogWithCatch(R.string.message_error_network_connect);
							break;
						case NETWORK_ERROR.HOST:
							showDialogWithCatch(R.string.message_error_network_host_not_exist);
							break;
						case NETWORK_ERROR.IO:
							showDialogWithCatch(R.string.message_error_network_io);
							break;

						// case STATUS.SERVER_SEND_UNKNOWN_TYPE:
						default:
							showDialogWithCatch(R.string.message_server_send_fail_unknown_type);

					}
				}
				else
				{
					switch (status)
					{
						case STATUS.SERVER_DB_CREATE_FAIL:
						case STATUS.SERVER_DB_INSERT_FAIL:
						case STATUS.SERVER_SEND_FAIL_DB:
						case STATUS.SERVER_SEND_FAIL_DB_LOAD:
						case STATUS.SERVER_SEND_FAIL_JSON:
						case STATUS.SERVER_SEND_FAIL_JSON_VALUE:
						case STATUS.SERVER_SEND_FAIL_VALUE_NULL:
						case STATUS.SERVER_SEND_UNKNOWN_TYPE:
							showDialogWithCatch(R.string.message_server_send_fail);
							break;
						case STATUS.SERVER_SEND_OK:
							showDialogWithCatch(R.string.message_server_send_ok);
							break;
						case NETWORK_ERROR.CONNECT:
							showDialogWithCatch(R.string.message_error_network_connect);
							break;
						case NETWORK_ERROR.HOST:
							showDialogWithCatch(R.string.message_error_network_host_not_exist);
							break;
						case NETWORK_ERROR.IO:
							showDialogWithCatch(R.string.message_error_network_io);
							break;
						default:
							showDialogWithCatch(R.string.message_server_send_fail_unknown_type);
					}
				}
			}
		});

	}

	@Override
	public void onNetworkTimeOut(Transaction transaction)
	{
		hideProgressDialog();
		showDialogWithCatch(R.string.message_time_out_network);

		debug("Network timeout -----------------");
	}

	@Override
	public void onIncomingCall(String number)
	{
	}

	protected void debugToastUsb(String message)
	{
		if (AppConfig.IS_DEBUG)
		{
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
	}

	protected void debug(String message)
	{
		if (AppConfig.IS_DEBUG)
		{
			Log.d(TAG, "@@@@@@@@@@@@ " + message);
		}
	}
}
